import service from '@/utils/public.js';

//直接上传
export const simpleFileUploadApi = (username, file) => {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('username', username); // 确保后端能够接收到 username

  return service.post('/upload', formData);
};

//初始化分块上传信息
export const InitiateMultipartUploadApi = (fileHash, fileName, userName, fileSize) => {
  // 创建 FormData 对象
  const formData = new FormData();
  formData.append('fileHash', fileHash);
  formData.append('fileName', fileName);
  formData.append('userName', userName);
  formData.append('fileSize', fileSize);

  // 发送请求
  return service({
    url: '/mpUploadInit',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data' // 设置 Content-Type
    }
  });
};

//分块上传
export const UploadMultipartApi = (chunkIndex, userName, uploadID, file) => {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('chunkIndex', chunkIndex);
  formData.append('userName', userName);
  formData.append('uploadID', uploadID);

  return service({
    url: '/mpUploadApart', // 确保 URL 正确
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data' // 设置 Content-Type
    }
  });
};

//分块上传结束
export const CompleteUploadApi = (fileHash, fileName, userName, fileSize, uploadId) => {
  // 创建 FormData 对象
  const formData = new FormData();
  formData.append('fileHash', fileHash);
  formData.append('fileName', fileName);
  formData.append('userName', userName);
  formData.append('fileSize', fileSize);
  formData.append('uploadId', uploadId);

  // 发送请求
  return service({
    url: '/mpUploadComplete', // 确保 URL 正确
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data' // 设置 Content-Type
    }
  });
};

//分页查询
export const getFileListApi = (username,page) => {
  return service({
    url: '/queryFileByNamePage',
    method: 'get',
    params: {
      username: username,
      page: page,
    }
  })
}


//根据hash下载文件
export const downLoadFileApi = (filehash) => {
  return service({
    url: '/downloadFile',
    method: 'get',
    params: {
      filehash: filehash,
    },
    responseType: 'blob'
  })
}